package com.nekonomics
package api.dto.response

import com.nekonomics.domain.detail.SkuSpecDetail
import zio.json.JsonEncoder


case class SkuSpecValue(
  id: Long,
  name: String,
) derives JsonEncoder


case class SkuSpecDto(
  id: Long,
  name: String,
  values: List[SkuSpecValue]
) derives JsonEncoder

object SkuSpecDto {
  def of(specs: List[SkuSpecDetail]) = {
    specs.groupMap(spec => (spec.specId, spec.name))(spec => SkuSpecValue(spec.specValueId, spec.value))
      .map{ case ((specId, specName), values) => SkuSpecDto(specId, specName, values.distinctBy(_.id)) }
      .toList
  }
}